package com.common.www.service.recycle;

import org.springframework.boot.base.jpa.service.BaseService;
import com.common.www.bean.entity.recycle.RecycleType;
import com.common.www.bean.entity.recycle.RecycleTypePrice;
import com.common.www.repository.recycle.RecycleTypePriceRepository;
import com.common.www.repository.recycle.RecycleTypeRepository;
import com.common.www.utils.TxtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by cwx183898 on 2017/12/14.
 */
@Service
@Transactional
public class RecycleTypePriceService extends BaseService<RecycleTypePrice, Long> {
    @Autowired
    private RecycleTypePriceRepository repository;

    @Override
    public RecycleTypePriceRepository getRepository() {
        return repository;
    }

    /**
     * 逻辑查询
     */
    public RecycleTypePrice getById(Long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("flag", 1);
        List<RecycleTypePrice> list = queryToList(map, null);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }

        return null;
    }

    /**
     * 逻辑查询
     */
    public List<RecycleTypePrice> getList(Map<String, Object> map, Pageable pageable) {
        if (map == null)
            map = new HashMap<>();
        map.put("flag", 1);
        List<RecycleTypePrice> list = queryToList(map, pageable);
        return list;
    }

    /**
     * 逻辑查询
     */
    public Page<RecycleTypePrice> getListPage(Map<String, Object> map, Pageable pageable) {
        if (map == null)
            map = new HashMap<>();
        map.put("flag", 1);
        Page<RecycleTypePrice> page = query(map, pageable);
        return page;
    }

    /**
     * 逻辑批量删除
     */
    public void deleteByIds(Long[] ids) {
        for (int i = 0; i < ids.length; i++) {
            deleteById(ids[i]);
        }
    }

    /**
     * 逻辑删除 节点，和所有相关子节点
     */
    public void deleteById(Long id) {
        RecycleTypePrice res = findOne(id);
        if (res != null) {
            res.setFlag(0);
            save(res);
        }
    }

    /**
     * 根据名称查询
     */
    public RecycleTypePrice getByName(String name) {
        if (TxtUtil.isEmpty(name))
            return null;
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("flag", 1);
        List<RecycleTypePrice> list = queryToList(map, null);
        if (list != null && list.size() > 0) {
            return list.get(0);
        }

        return null;
    }


    ////////////////////////////////


}
